package fr.dauphine.spring.ctl;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import fr.dauphine.spring.bo.Client;
import fr.dauphine.spring.bo.Commande;
import fr.dauphine.spring.bo.Produit;
import fr.dauphine.spring.dao.ClientDAO;
import fr.dauphine.spring.dao.CommandeDAO;
import fr.dauphine.spring.dao.ProduitDAO;

@Controller
@RequestMapping("commande.html")
@SessionAttributes("client")
public class CommandeController {
	@Autowired
	ClientDAO clientDAO;
	@Autowired
	CommandeDAO commandeDAO;
	@Autowired
	private ProduitDAO produitDAO;

	// protected void initBinder(HttpServletRequest request,
	// ServletRequestDataBinder binder)
	// throws Exception {
	// binder.registerCustomEditor(Map.class, "listeProduits", new
	// CustomCollectionEditor(
	// Map.class) {
	// protected Object convertElement(Object element) {
	// String name = "";
	// if (element instanceof String)
	// name = (String) element;
	// return name != null ? name : null;
	// }
	// });
	// }

	@RequestMapping(method = RequestMethod.GET, params = {})
	public String affichage(@ModelAttribute("client") Client client, ModelMap model) {
		try {
			Commande.setListeProduits((ArrayList<Produit>) produitDAO.getAll());
			System.out.println("id client :" + client.getId());
			clientDAO.getCommandes(client);
			System.out.println(produitDAO);
			produitDAO.save(new Produit("VTT", 220));
			produitDAO.save(new Produit("pc", 699));
			produitDAO.save(new Produit("livre", 13));
			produitDAO.save(new Produit("oreiller", 4));
			if (client.getLstCommandes().isEmpty()) {
				System.out.println("===Nouvelle commande===");
				Commande c = new Commande();
				model.addAttribute("commande", c);
				client.getLstCommandes().add(c);
			} else {
				System.out.println("Affichage commandes");
				model.addAttribute("commande", client.getLstCommandes().get(0));
			}
			model.addAttribute("client", client);
			return "commande";
		} catch (Exception ex) {
			ex.printStackTrace();
			return "erreur";
		}
	}

	@RequestMapping(method = RequestMethod.GET, params = { "new" })
	public String affichage(@ModelAttribute("client") Client client, ModelMap model,
			@RequestParam("new") String newCommande) {
		try {
			System.out.println("Nouvelle commande!");
			model.addAttribute("commande", new Commande());
			return "commande";
		} catch (Exception ex) {
			ex.printStackTrace();
			return "erreur";
		}
	}

	@RequestMapping(method = RequestMethod.GET, params = { "idCommande" })
	public String affichageCommande(@ModelAttribute("client") Client client, ModelMap model,
			@RequestParam("idCommande") String id) {
		try {
			System.out.println("id commande :" + id);
			model.addAttribute("commande", commandeDAO.getById(id));
			return "commande";
		} catch (Exception ex) {
			ex.printStackTrace();
			return "erreur";
		}
	}

	@RequestMapping(params = "validation=Valider", method = RequestMethod.POST)
	public String validation(@ModelAttribute("commande") Commande commande, ModelMap model,
			@RequestParam("idClient") String id, @RequestParam("prodSelected") String[] ids)
			throws Exception {
		System.out.println("id to find : " + id);
		Client client = clientDAO.get(id);
		for (int i = 0; i < ids.length; i++) {
			System.out.println("ids " + ids[i]);
		}
		clientDAO.getCommandes(client);
		commandeDAO.save(commande);
		if (client.getLstCommandes() != null) {
			client.getLstCommandes().add(commande);
		} else {
			client.setLstCommandes(new ArrayList<Commande>());
			client.getLstCommandes().add(commande);
		}
		clientDAO.save(client);
		return "commande";
	}

	@RequestMapping(params = "validation=Annuler", method = RequestMethod.POST)
	public String validation() {
		System.out.println("Annulation commande");
		return "redirect:clients.html";
	}
}
